package com.demoo.zjsu.service.smartwebsite;

import com.demoo.zjsu.string.BaseStrings;
import com.demoo.zjsu.string.smartwebsite.WebsiteStrings;
import com.demoo.zjsu.util.HttpClientUtil.ClientImgDownloader;
import com.demoo.zjsu.util.HttpClientUtil.ClientSender;
import com.demoo.zjsu.util.HttpClientUtil.RequestConfigResolver;
import com.demoo.zjsu.util.HttpClientUtil.pool.DownLoad;
import com.demoo.zjsu.util.HttpClientUtil.pool.IdleConnectionEvictor;
import com.demoo.zjsu.util.stringutil.StringUtil;
import org.apache.log4j.Logger;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Map;

/**
 * Created by jshjsh on 2016/8/16.
 */
@Service
public abstract class IFactory {

    public abstract Page getPage();

    public abstract void resolver();

    public abstract Elements getMain(Document document);

    /**
     * 第一次过滤时加入的额外过滤器
     * @param elements
     */
    public abstract void filterElse(Elements elements);

    public void filterMain(Elements elements) {
        elements.select("[class]").removeAttr(WebsiteStrings.CLASS);
        elements.select("[style]").removeAttr(WebsiteStrings.STYLE);
        elements.select("[id]").removeAttr(WebsiteStrings.ID);
        elements.select("[script]").removeAttr(WebsiteStrings.SCRIPT);
//        elements.select("[border]").removeAttr(WebsiteStrings.BORDER);
        elements.select("[cellspacing]").removeAttr(WebsiteStrings.CELLSPACING);
        elements.select("[cellpadding]").removeAttr(WebsiteStrings.CELLPADDING);
        elements.select("[bordercolor]").removeAttr(WebsiteStrings.BORDERCOLOR);
        elements.select("[width]").removeAttr(WebsiteStrings.WIDTH);
        elements.select("[height]").removeAttr(WebsiteStrings.HEIGHT);
        elements.select(WebsiteStrings.SCRIPT).remove();
        elements.select("[onload]").removeAttr("onload");
    }

    public void dealPhotoAndFile(Elements main) {
        Elements img = getPhoto(main);
        img.select("img[src$=.jpg]").attr("width","100%");
        img.select("img[src$=.png]").attr("width","100%");
        img.select("img[src$=.JPG]").attr("width","100%");
        dealPhoto(img, WebsiteStrings.SRC);
        Elements file = getFile(main);
        dealPhoto(file, WebsiteStrings.HREF);
    }


    /**
     * 添加选中的图片
     * @param elements
     */
    public abstract void addPhoto(Elements elements);

    /**
     * 添加选中的文件
     * @param elements
     */
    public abstract void addFile(Elements elements);


    public Elements getPhoto(Elements elements) {
        Elements img = elements.select("img");
        addPhoto(elements);
        return img;
    }

    /**
     * 添加选中的文件
     * @param elements
     */
    public Elements getFile(Elements elements) {
        Elements img = elements.select("a[href$=.gif]");
        img.addAll(elements.select("a[href$=.pdf]"));
        img.addAll(elements.select("a[href$=.doc]"));
        img.addAll(elements.select("a[href$=.xls]"));
        img.addAll(elements.select("a[href$=.docx]"));
        addFile(elements);
        return img;

    }



    /**
     * 有待于测试
     *
     * @param elements 类型
     * @param category
     */

    public void dealPhoto(Elements elements, String category) {

        for (Element element : elements) {
            String abs_url = element.attr("abs:"+category).trim();
            String url = element.attr(category).trim()
                    .replace("http://www.zjgsu.edu.cn", "")
                    .replace("http://zhaoban.zjsu.edu.cn", "")
                    .replace("http://www.zjsu.edu.cn", "");
            element.attr(category, url);
            Map<String, String> map = StringUtil.cutIp(url);
            String filename = map.get("fileName");
            String filepath = BaseStrings.URL + map.get("filePath");
            DownLoad.download(abs_url, filepath, filename);
        }

    }
}
